Repository navigation
Compare uniqueItems items without copying them - #1305
Conversation
comparisonKey built a BigDecimal for every number and copied every nested array and object, so all arrays paid for the numeric-equality fix, not only arrays that mix integers and decimals. Wrap each item in a key that walks the node and compares numbers by value instead: whole numbers that fit in a long as longs, other doubles as doubles, and only other BigIntegers and BigDecimals as BigDecimals. Whole doubles at or above 2^63 are compared by their exact value, the same as smaller whole doubles, rather than by their shortest decimal form. Make decimalsWithDifferentScaleAreDuplicates read BigDecimals so it no longer passes without numeric folding, fix the comparisonKey Javadoc indentation by rewriting it, and add tests for the new comparison paths. Fixes networknt#1302
|
Code review (xhigh · 8 findings) Thanks for this. For ordinary arrays the speedup holds up, and the code is easy to follow. I found three problems the existing suites don't cover, and I'd like those fixed before merging. Verification:
Please change these:
A fix for 1 and 2. I tried this on case DOUBLE:
return Double.hashCode(node.doubleValue());
default: // BIG
BigDecimal value = node.decimalValue();
double d = value.doubleValue(); // BigDecimal never throws, it returns ±Infinity
if (Double.isFinite(d) && doubleDecimal(d).compareTo(value) == 0) {
// Exactly equal to a double, so it must share that double's hash.
return Double.hashCode(d);
}
return value.stripTrailingZeros().hashCode();Here, Tests, please:
Optional:
The same applies to #1306. Once items 1–3 are in on both PRs, I'm happy to merge them together. |
… doubles BigIntegerNode and DecimalNode throw from doubleValue() in Jackson 3 when the value is outside the double range, so hash a BIG number from its BigDecimal. A BIG number that isn't exactly a double's value now hashes its own exact value, so integers that round to the same double no longer share a bucket. Whole doubles of 2^53 and above go back to comparing by decimalValue(), their shortest decimal form, as before. Changing that belongs with const and enum in networknt#1293.
|
Thanks for the detailed review. Items 1–3 are in: 8f7408b here and c2cd3f9 on #1306. 1 and 2: I used your hash. A BIG number takes 3: Reverted. Comparison goes back to Tests added:
Item 7 goes away with the revert, and the Javadoc now says the value is
|
|
Code review (xhigh · 3 findings) Thanks. 8f7408b fixes items 1–3, and I checked it merged into the current
#1306 (c2cd3f9) on Please change this one: the double -2^63. I fuzzed 2.39 million number pairs against the old
Changing the check to Optional:
Items 4 and 6 from the last round will be a follow-up issue. The same applies to #1306. Once the -2^63 fix is in on both PRs, I'm happy to merge them together. |
|
Follow-up for items 4 and 6 is now #1307. |
|
Thanks, fixed in ba114aa (and bc140e7 on #1306).
I also took both optional items: the Javadoc on
|
stevehu
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround. I re-reviewed ba114aa (#1305) and bc140e7 (#1306); the two are identical apart from the Jackson imports.
Verification
- The -2^63 fix works.
[-2^63, its shortest form]is a duplicate and[-2^63, 9223372036854775616]is unique, matching base andconst. All 15 -2^63 edge cases match base on Java 8, 17, 21 and 25. - Revert check: with the previous commit's validator,
minusTwoToSixtyThreeAsADoubleIsComparedByItsShortestDecimalFormfails on JDK 25 and Java 8. - On
master(JDK 17, 25) and2.x(Java 8, 21):- The JSON Schema Test Suite (7261), the UniqueItems tests (50 + 17), and the const/enum tests pass.
- The 253k-input and 2.39M-input fuzzes show 0 disagreements.
- The DoS probe stays at 4–20 ms for 20k items.
Optional, not blocking
- The test Javadoc says the shortest form is
-9223372036854776000"on JDK 19+". Corretto 8 and 17 give the same form, so the JDK qualifier can go. - One more assertion would pin the pair on the boundary that moved:
assertTrue(valid(array().add(Long.MIN_VALUE).add(-0x1p63))). value > -0x1p63relies ondecimalValue()of -2^63 being belowLong.MIN_VALUE. That holds on every JDK I tested. Deciding that single value from its decimal form would remove the assumption, if you ever touch this again.
Fixes #1302
UniqueItemsValidatornow compares items through a small key that walks each node in place, instead of building acomparisonKeycopy. Numbers are still compared by value, so[1, 1.0]is still a duplicate, but arrays without mixed number types no longer pay for it.How numbers are compared:
decimalValue(), as before. For a double that is its shortest decimal form, so1e30equals10^30and a double read from0.1equals aBigDecimal0.1.long(ints, longs, and whole doubles,BigIntegers andBigDecimals) are compared aslongs.doubles.BigIntegers andBigDecimals outside that range are compared asBigDecimals.Double.hashCodeof its value. ABigIntegerorBigDecimaloutside the long range takes that same hash only when it is exactly a double's value; otherwise it hashes its own exact value. So equal numbers of different types land in the same bucket, while integers that only round to the same double, such as 2^70 and 2^70 + 1, don't. The hash never callsdoubleValue()on a big node, which Jackson 3 would throw from for values like 10^400.There is no behaviour change from the current code. Fuzzing 400,000 number pairs (large whole doubles, powers of ten, big integers near them, trailing-zero decimals, and each value in another type) against the current
comparisonKeygives the same verdict for every pair, both at the top level and nested.Items deferred from the #1301 review:
decimalsWithDifferentScaleAreDuplicatesnow reads the instance withUSE_BIG_DECIMAL_FOR_FLOATS, so1.0and1.00stayBigDecimals with different scales. It fails without numeric folding onmaster.comparisonKeyJavadoc is rewritten for the new key, with the indentation fixed.New tests in
UniqueItemsNumericEqualityTest: double vsBigDecimal,0vs-0.0, numbers outside the long range, large whole doubles ([1e30, 10^30]is a duplicate, the long 2^60 and the double 2^60 are not), 1,000 consecutive integers from 2^70 (all unique, then one repeated),[10^400]nested and at the top level, and[1e400, 1e401]/[1e400, 1e400]withUSE_BIG_DECIMAL_FOR_FLOATS, and object property order. On the first version of this PR, the large-double test failed on both branches and the 10^400 test threw onmaster. The 2^70 test checks correctness only, so it doesn't depend on timing.Timing with schema
{"uniqueItems":true}: 50 warm-up runs, then the best of 5 rounds of 20 runs, and the median of 3 JVM runs. Times in ms.master, JDK 25:comparisonKey)i.5)[i, i+1.25, {"a":i}])2.x, JDK 17 (I don't have a Java 8 JDK here):comparisonKey)i.5)Against the current code it is 1.5–3x faster on
masterand 1.4–4.2x on2.x. It's still a bit slower than plain node equality, mostly from one key object per item and classifying each number. The objects numbers were the noisiest between runs. None of these instances goes through the big-number hash.Consecutive integers from 2^70, which share a double in runs of 2^18. The first two columns are from your review; the last is one run with no warm-up:
master) / 4.6 s (2.x)master) / 19 ms (2.x)master) / 108 ms (2.x)mvn verifypasses, including the JSON Schema Test Suite: 8768 tests on JDK 25.